package com.study.springbootsecurity.controller;

import com.google.zxing.WriterException;
import com.study.springbootsecurity.config.properties.JwtConfigProperties;
import com.study.springbootsecurity.domain.dto.QRCodeDTO;
import com.study.springbootsecurity.domain.dto.query.LoginDTO;
import com.study.springbootsecurity.domain.dto.query.SmsCodeLoginDTO;
import com.study.springbootsecurity.entity.SysUser;
import com.study.springbootsecurity.entity.business.RefreshResponse;
import com.study.springbootsecurity.entity.business.SmsCodeQuery;
import com.study.springbootsecurity.service.QrCodeService;
import com.study.springbootsecurity.service.SmsCodeService;
import com.study.springbootsecurity.service.SysUserService;
import com.study.springbootsecurity.service.impl.TokenService;
import com.study.springbootsecurity.util.QrCodeUtil;
import com.study.springbootsecurity.util.ResponseUtil;
import com.study.springbootsecurity.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.RandomAccess;
import java.util.UUID;

/**
 * @Description: TODO
 * @author: Huangjianyun
 * @date: 2025-06-03 19:44
 */
@Slf4j
@RestController
@RequestMapping("/api/user")
@Tag(name = "系统用户管理", description = "提供用户的增删改查功能")
public class SysUserController {
    @Autowired
    private SysUserService userService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private JwtConfigProperties jwtConfigProperties;
    @Autowired
    private SmsCodeService smsCodeService;
    @Autowired
    private QrCodeService qrCodeService;



    @GetMapping("/getByUsername")
//    @PreAuthorize("hasAuthority(T(com.study.springbootsecurity.constants.PermissionContant).USER_VIEW || hasAuthority(T(com.study.springbootsecurity.constants.PermissionContant).ALL))")
    @PreAuthorize("hasAuthority(T(com.study.springbootsecurity.constants.PermissionContant).ALL)")

    @Operation(summary = "查询用户", description = "根据用户名查询系统用户",
    parameters = {
            @Parameter(name = "username",description = "用户名")
    })
    public Result<SysUser> getByUsername(@RequestParam("username") String username) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            log.error("SecurityContext中没有认证对象");
        } else {
            log.info("认证对象: {}", authentication);
        }
        return Result.success(userService.getByUsername(username));
    }

    @PostMapping("/login")
    @Operation(summary = "login", description = "用户登录")
    public Object login(@RequestBody @Valid LoginDTO loginDTO) {
        return null;
    }

    @PostMapping("/logout")
    @Operation(summary = "logout", description = "用户退出登录")
    public Object logout() {
        return null;
    }

    @PostMapping("/refreshToken")
    @Operation(summary = "刷新token",description = "刷新token")
    public Result<?> refreshToken(HttpServletRequest request, HttpServletResponse response, @CookieValue(name = "refresh_token", required = false) String refreshToken) {
        RefreshResponse refreshResponse = tokenService.refreshToken(request, response, refreshToken, jwtConfigProperties);
        return Result.success(refreshResponse);
    }

    @PostMapping("/send/phoneCode")
    @Operation(summary = "发送手机验证码",description = "发送手机验证码")
    public Result sendPhoneCode(@RequestBody SmsCodeQuery smsCodeQuery) {
        smsCodeService.sendSmsCode(smsCodeQuery);
        return Result.success();
    }

    @PostMapping("/smsLogin")
    @Operation(summary = "手机验证码登录",description = "手机验证码登录")
    public Result smsLogin(@RequestBody SmsCodeLoginDTO smsCodeLoginDTO) {
        // 登录业务逻辑 登录过滤器里面
        return null;
    }

    @PostMapping("/generateQrCode")
    @Operation(summary = "生成二维码",description = "生成二维码")
    public Result<QRCodeDTO> generateQrCode(HttpServletResponse response) {
        return Result.success(qrCodeService.generateQRCode());
    }

//    @PostMapping("/scanQrCode")
//    @Operation(summary = "扫描二维码",description = "扫描二维码")
//    public Result scanQrCode() {
//        // 还未编写
//        return Result.success();
//    }



}
